﻿using System;
using DL.Animation;
using DL.Characters.Player.FSM.StateMachine;
using DL.Tools;
using UnityEngine;
using UnityEngine.InputSystem;

namespace DL.Characters.Player.FSM.State
{
    [Obsolete]
    public class PlayerLocomotion8DirState : PlayerOnGroundState
    {
        public PlayerLocomotion8DirState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(playerStateMachine, property)
        {
        }

        public override void Enter()
        {
            base.Enter();
            ReusableData.CanRotation = false;
            Animator.SetBool(AnimatorParameter.HasInput, true);
            Animator.SetFloat(AnimatorParameter.Lock, 1.5f);
            ReusableData.InputMult = ReusableData.Running ? 4.1f : 2.1f;
            ReusableData.RotationTime = ReusableData.Running ? 0.07f : 0.05f;
        }

        public override void Exit()
        {
            base.Exit();
            ReusableData.CanRotation = true;
            Animator.SetFloat(AnimatorParameter.Lock, -0.1f);
        }

        public override void HandleInput()
        {
            base.HandleInput();
            Animator.SetFloat(AnimatorParameter.Movement, ReusableData.InputDirection.sqrMagnitude * ReusableData.InputMult, 0.35f,
                Time.deltaTime);
            Animator.SetFloat(AnimatorParameter.Horizontal, ReusableData.InputDirection.x, 0.2f, Time.deltaTime);
            Animator.SetFloat(AnimatorParameter.Vertical, ReusableData.InputDirection.y, 0.2f, Time.deltaTime);
        }
        
         public override void OnAnimationExitEvent()
        {
            Animator.SetFloat(AnimatorParameter.Movement, 0);
            StateMachine.ChangeState<PlayerLocomotionState>();
        }

        public override void Update()
        {
            if (StateMachine.MovementController.CalculateTurnAngles(ReusableData.InputDirection))
            {
                MoveController.TurnTowards(ReusableData.CurrentAngle, ReusableData.RotationAngle, 100f);
                Animator.SetFloat(AnimatorParameter.DeltaAngle, ReusableData.TargetAngle);
            }
            else
            {
                Animator.SetFloat(AnimatorParameter.DeltaAngle, 0f);
            }
        }

        protected override void AddInputActionCallBacks()
        {
            StateMachine.InputSystem._playerInputActions.Movement.canceled += EnterIdleStart;
        }
        
        protected override void RemoveInputActionCallBacks()
        {
            StateMachine.InputSystem._playerInputActions.Movement.canceled -= EnterIdleStart;
        }
        
        private GameTimer m_GameTimer;
        protected void EnterIdleStart(InputAction.CallbackContext context)
        {
            StateMachine.InputSystem._playerInputActions.Movement.started += CancelEnterIdleStart;
            m_GameTimer = NewTimerManager.Inst.GetTimer(.2f, EnterIdle);
        }
        protected virtual void EnterIdle()
        {
            StateMachine.InputSystem._playerInputActions.Movement.started -= CancelEnterIdleStart;
            Animator.CrossFadeInFixedTime("Walk_Stop", 0.25f);
        }
        
        protected void CancelEnterIdleStart(InputAction.CallbackContext context)
        {
            NewTimerManager.Inst.CalcelTimer(m_GameTimer);
        }
        
    }
}